import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import thothCommonClasses.*;

public class communicator {
	final int port = 9720;
	private InetAddress addr;
	Socket sendSocket = null;
	ServerSocket serverSocket = null;
	
	public communicator() throws UnknownHostException
	{
		addr = InetAddress.getByName("localhost");
	}
	public static void main (String args[])
	{
		
	}
	
	public void CloseConection() throws IOException
	{
		if(sendSocket != null)
			sendSocket.close();
		if(serverSocket != null)
			serverSocket.close();
	}
	
	public DataList GetDataList()
	{
		DataList data = new DataList(); 
		try  
		{  
			if(serverSocket == null ||  serverSocket.isClosed())
			{
			    serverSocket = new ServerSocket(port); 
	            System.out.println("Server Started: " + serverSocket); 
			}
			Socket socket = serverSocket.accept();
			
            System.out.println("Connection accepted, socket: " + socket); 
		    
		    System.out.println("socket = " + socket); 
		    ObjectInputStream FromServer = new ObjectInputStream(socket.getInputStream()); 
		    data = (DataList)FromServer.readObject(); 
		    System.out.println("succeeded"); 
		    System.out.println(data.toString());
		    
		    socket.close();
		    serverSocket.close();
	    }  
	    catch(Exception e)  
	    { 
	    	e.printStackTrace(); 
	    }
		return data;
	}
	
	public void SendData(DataList data)
	{
		try
		{    
			System.out.println("addr = " + addr);
			if(sendSocket == null || sendSocket.isClosed())
				sendSocket = new Socket(addr, port);  
	         System.out.println("Connection accepted, socket: " + sendSocket);
	         ObjectOutputStream ToClient = new ObjectOutputStream(sendSocket.getOutputStream());
	         
	         System.out.println("writing " + data.toString());
	         
	         ToClient.writeObject(data);
	                    
	         System.out.println("succeeded");	
	         System.out.println("closing..."); 
	         sendSocket.close(); 
	     }  
	      catch (EOFException f)  
	      { 
	           System.exit(0); 
	      }  
	      catch(Exception e)  
	      { 
	           e.printStackTrace(); 
	      } 
	}
	
	public Header GetHeader()
	{
		Header data = new Header();
		ObjectInputStream inputStream = null;

		try  
		{  
			if(serverSocket == null ||  serverSocket.isClosed())
			{
			    serverSocket = new ServerSocket(port); 
	            System.out.println("Server Started: " + serverSocket); 
			}
			Socket socket = serverSocket.accept();
            
		    System.out.println("socket = " + socket); 
		    ObjectInputStream FromServer = new ObjectInputStream(socket.getInputStream()); 
		    data = (Header)FromServer.readObject(); 
		    System.out.println("succeeded"); 
		    System.out.println(data.toString());
		    
		    socket.close();
		    serverSocket.close();
	    }  
	    catch(Exception e)  
	    { 
	    	e.printStackTrace(); 
	    }
		 return data;
	}
	
	public void SendHeader(Header data)
	{
		try
		{   
			System.out.println("addr = " + addr); 
			if(sendSocket == null || sendSocket.isClosed())
				sendSocket = new Socket(addr, port);  
	         System.out.println("Connection accepted, socket: " + sendSocket); 
	         ObjectOutputStream ToClient = new ObjectOutputStream(sendSocket.getOutputStream());
	         
	         System.out.println("writing " + data.toString());
	         
	         ToClient.writeObject(data);
	                    
	         System.out.println("succeeded");	
	         System.out.println("closing..."); 
	         sendSocket.close(); 
	     }  
	      catch (EOFException f)  
	      { 
	           System.exit(0); 
	      }  
	      catch(Exception e)  
	      { 
	           e.printStackTrace(); 
	      }
	}
	
	public Parameters GetParameters()
	{
		Parameters data = new Parameters();
		ObjectInputStream inputStream = null;

		try  
		{  
			if(serverSocket == null ||  serverSocket.isClosed())
			{
			    serverSocket = new ServerSocket(port); 
	            System.out.println("Server Started: " + serverSocket); 
			}
			Socket socket = serverSocket.accept();
			
		    System.out.println("socket = " + socket); 
		    ObjectInputStream FromServer = new ObjectInputStream(socket.getInputStream()); 
		    data = (Parameters)FromServer.readObject(); 
		    System.out.println("succeeded"); 
		    System.out.println(data.toString());
		    socket.close();
	    }  
	    catch(Exception e)  
	    { 
	    	e.printStackTrace(); 
	    }
		/*
		  try  

	        {  
	             ServerSocket s = new ServerSocket(port); 

	             System.out.println("Server Started: inside communicator " + s); 

	             Socket socket = s.accept(); 

	             System.out.println("Connection accepted, socket: " + socket); 
	             
	             InputStream is = socket.getInputStream();    
	             ObjectInputStream ois = new ObjectInputStream(is); 
	             Object obj = null;
	             
	             while ((obj = ois.readObject()) != null) {
	                 
	                 if (obj instanceof Parameters) {
	                     data = (Parameters)obj;
	                 }
	                 
	             }
	             
	             
                 System.out.println("succeeded"); 

	             System.out.println("closing..."); 

//	             ToClient.close(); 

	             socket.close(); 

	        }  

	        catch(Exception e)  

	        { 

	             e.printStackTrace(); 

	        }
	        */
		  return data;
	}
	
	public void SendParameters(Parameters data)
	{
		try
		{    
			System.out.println("addr = " + addr); 
			if(sendSocket == null || sendSocket.isClosed())
				sendSocket = new Socket(addr, port);  
	         System.out.println("Connection accepted, socket: " + sendSocket);
	         ObjectOutputStream ToClient = new ObjectOutputStream(sendSocket.getOutputStream());
	         
	         System.out.println("writing " + data.toString());
	         
	         ToClient.writeObject(data);
	                    
	         System.out.println("succeeded");	
	         System.out.println("closing..."); 
	         sendSocket.close(); 
	     }  
	      catch (EOFException f)  
	      { 
	           System.exit(0); 
	      }  
	      catch(Exception e)  
	      { 
	           e.printStackTrace(); 
	      }
	}
}
